﻿using Learun.Util.SqlSugar;
using Learun.Util;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using DbType = System.Data.DbType;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    public class ec_accessService
    {
        #region 仓储
        Repository<ec_projectEntity> _projectRepository => new Repository<ec_projectEntity>();
        Repository<ec_CableEntity> _cableRepository => new Repository<ec_CableEntity>();
        #endregion
        /// <summary>
        /// 获取页面显示列表数据
        /// <summary>
        /// <param name="queryJson">查询参数</param>
        /// <param name="NeedLogin">默认true。表示需要用户登录</param>
        /// <returns></returns>
        public IEnumerable<ec_accessEntity> GetList(string queryJson, bool NeedLogin = true)
        {
            try
            {
                string curUser = "";
                if (NeedLogin)
                {
                    try
                    {
                        curUser = LoginUserInfo.Get().userId;
                    }
                    catch (Exception)
                    {

                        return null;
                    }
                }


                var queryParam = queryJson.ToJObject();
                var strSql = new StringBuilder();
                strSql.Append("SELECT ");
                strSql.Append(@" t.* ");
                if (queryParam["ProjectId"].IsEmpty())
                {
                    strSql.Append("  FROM ec_access t ");
                }
                else
                {
                    //var ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(queryParam["ProjectId"].ToString());
                    var ProjectEntity = _projectRepository.GetById(queryParam["ProjectId"].ToString());
                    strSql.Append($"  FROM ec_access_{ProjectEntity.ProjectIndex} t ");
                }
                strSql.Append($"  WHERE 1=1 ");


                // 虚拟参数
                //var dp = new DynamicParameters(new { });
                var dp = new List<SugarParameter>();
                if (curUser != "")
                {
                    dp.Add(new SugarParameter("UserID", curUser, DbType.String));
                    strSql.Append(" AND (t.UserID = @UserID) ");
                }
                if (!queryParam["EntityTableID"].IsEmpty())
                {
                    dp.Add(new SugarParameter("EntityTableID", queryParam["EntityTableID"].ToString(), DbType.String));
                    strSql.Append(" AND (t.EntityTableID = @EntityTableID) ");
                }
                if (!queryParam["EntityID"].IsEmpty())
                {
                    dp.Add(new SugarParameter("EntityID", queryParam["EntityID"].ToString(), DbType.String)); //如果是等于 就不需要%%
                    strSql.Append(" AND (t.EntityID = @EntityID) ");
                }

                //return this.BaseRepository().FindList<ec_accessEntity>(strSql.ToString(), dp);
                return SqlSugarHelper.Db.SqlQueryable<ec_accessEntity>(strSql.ToString()).AddParameters(dp).ToList();
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }

        }

    }
}
